package com.open.capacity.workflow3.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.open.capacity.commons.Result;
import com.open.capacity.model.system.SysUser;
import com.open.capacity.user.service.SysUserRelationService;
import com.open.capacity.user.service.SysUserService;
import com.open.capacity.utils.SysUserUtil;
import com.open.capacity.workflow3.entity.NwfSchemeAuth;
import com.open.capacity.workflow3.entity.NwfSchemeinfo;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import com.open.capacity.workflow3.entity.NwfScheme;
import com.open.capacity.workflow3.service.NwfSchemeService;

import com.open.capacity.commons.PageResult;

import javax.validation.Valid;

/**
 * @author
 * @email
 * @date 2023-11-02 21:21:15
 */
@RestController
@RequestMapping("nwfschemes")
@Api(tags = "")
public class NwfSchemeController {

    @Autowired
    private NwfSchemeService schemeService;

    @Autowired
    private SysUserService userService;

    @Autowired
    private SysUserRelationService userRelationService;


    @RequestMapping("/getInfoPageList")
    public PageResult getInfoPageList(@RequestParam Map<String, Object> param) {
        PageResult pageResult = schemeService.getInfoPageList(param);
        return pageResult;
    }

    @GetMapping("/getList")
    public Result getList() {
        List<NwfSchemeinfo> data = schemeService.getInfoList();
        return Result.succeed(data);
    }

    @GetMapping("/getMyInfoList")
    public Result getMyInfoList(String userId) {
        SysUser userInfo = SysUserUtil.getLoginAppUser();
        if (userId != null && !userId.equals(userInfo.getId())) {
            userInfo = userService.findById(userId);
        }
        List<NwfSchemeinfo> data = schemeService.getInfoList(userInfo);
        return Result.succeed(data);
    }

    @GetMapping("/getFormData")
    public Result getFormData(String code) {
        NwfSchemeinfo schemeInfoEntity = schemeService.getInfoEntityByCode(code);
        if (schemeInfoEntity == null) {
            return Result.succeed(null, "ok");
        }
        NwfScheme schemeEntity = schemeService.getSchemeEntity(schemeInfoEntity.getSchemeId());
        List<NwfSchemeAuth> nWFSchemeAuthList = schemeService.getAuthList(schemeInfoEntity.getId());

        Map<String, Object> map = new HashMap<>();
        map.put("info", schemeInfoEntity);
        map.put("scheme", schemeEntity);
        map.put("authList", nWFSchemeAuthList);
        return Result.succeed(map);
    }

    @GetMapping("/getSchemePageList")
    public PageResult getSchemePageList(@RequestParam Map<String, Object> param, String schemeInfoId) {
        PageResult pageResult = schemeService.getSchemePageList(schemeInfoId, param);
        return pageResult;
    }

    @GetMapping("/getScheme")
    public Result getScheme(String schemeId) {
        NwfScheme data = schemeService.getSchemeEntity(schemeId);
        return Result.succeed(data);
    }

    @PostMapping("/exportScheme")
    public void exportScheme(String code) {
        NwfSchemeinfo schemeInfoEntity = schemeService.getInfoEntityByCode(code);
        if (schemeInfoEntity != null) {
            NwfScheme schemeEntity = schemeService.getSchemeEntity(schemeInfoEntity.getSchemeId());
            List<NwfSchemeAuth> nWFSchemeAuthList = schemeService.getAuthList(schemeInfoEntity.getId());
//            FileDownHelper.DownLoadString(new
//            {
//                info = schemeInfoEntity,
//                        scheme = schemeEntity,
//                        authList = nWFSchemeAuthList
//            }.ToJson(), schemeInfoEntity.F_Name + ".lrscheme");
        }
    }

    @PostMapping("/executeImportScheme")
    public Result executeImportScheme(String templateId, String fileId, int chunks, String ext) {
        SysUser userInfo = SysUserUtil.getLoginAppUser();
//        String path = annexesFileIBLL.SaveAnnexes(fileId, fileId + "." + ext, chunks, userInfo);
//        if (StringUtils.isNotEmpty(path)) {
//            String data = DirFileHelper.ReadText(path);
//            DirFileHelper.DeleteFile2(path);
//            if (!String.IsNullOrEmpty(data)) {
//                NWFSchemeModel nWFSchemeModel = data.ToObject < NWFSchemeModel > ();
//                if (schemeService.GetInfoEntityByCode(nWFSchemeModel.info.F_Code) != null) {
//                    nWFSchemeModel.info.F_Code = Guid.NewGuid().ToString();
//                }
//                schemeService.SaveEntity("", nWFSchemeModel.info, nWFSchemeModel.scheme, nWFSchemeModel.authList);
//            }
//            return Result.succeed("导入成功");
//        }
        return Result.failed("导入模板失败!");
    }

    @PostMapping("/saveForm")
    public Result saveForm(String id, String schemeInfo, String shcemeAuth, String scheme, Integer type) {
        NwfSchemeinfo schemeInfoEntity = JSONObject.parseObject(schemeInfo, new TypeReference<NwfSchemeinfo>() {
        });
        List<NwfSchemeAuth> nWFSchemeAuthList = JSONObject.parseObject(shcemeAuth, new TypeReference<List<NwfSchemeAuth>>() {
        });
        NwfScheme schemeEntity = new NwfScheme();
        schemeEntity.setContent(scheme);
        schemeEntity.setType(type);
        NwfSchemeinfo schemeInfoEntityTmp = schemeService.getInfoEntityByCode(schemeInfoEntity.getCode());
        if (schemeInfoEntityTmp != null && !schemeInfoEntityTmp.getId().equals(id)) {
            return Result.failed("流程编码重复");
        }
        schemeService.save(id, schemeInfoEntity, schemeEntity, nWFSchemeAuthList);
        return Result.succeed("保存成功！");
    }

    @RequestMapping("/deleteForm")
    public Result deleteForm(String keyValue) {
        schemeService.delete(keyValue);
        return Result.succeed("删除成功！");
    }

    @RequestMapping("/updateSate")
    public Result updateSate(String keyValue, Integer state) {
        schemeService.updateState(keyValue, state);
        return Result.succeed(((state == 1) ? "启用" : "禁用") + "成功！");
    }

    @RequestMapping("/updateScheme")
    public Result updateScheme(String schemeInfoId, String schemeId) {
        schemeService.updateScheme(schemeInfoId, schemeId);
        return Result.succeed("更新成功！");
    }

}
